﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Collections.Specialized;

namespace POSSystem.BLL
{
    public class PurchaseOrders :_PurchaseOrders, IPayment
    {
        public static bool IsPurchaseNoExisted(String number)
        {
            try
            {
                PurchaseOrders po = new PurchaseOrders();
                po.Where.PurchaseOrderNo.Value = number;
                po.Query.Load();

                return po.RowCount > 0;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("PurchaseOrders.cs", "IsPurchaseNumberNoExisted", ex.StackTrace);
                throw new Exception("PurchaseOrders.IsPurchaseNumberNoExisted(): " + ex.Message);
            }
        }

        #region IPayment Members

        public void Pay(decimal amt, int paymentMethoidID, int userID)
        {
            if (amt <= 0)
                return;
            this.PaidAmount += amt;
            this.LastModifiedByUser = userID;
            this.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
        }

        public void PayFull(int paymentMethoidID, int userID)
        {
            decimal amt = TotalAmount - PaidAmount;
            if (amt <= 0)
                return;
            this.PaidAmount = this.TotalAmount;
            this.LastModifiedByUser = userID;
            this.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
        }

        #endregion

        public static PurchaseOrders GetUnPaidPOs()
        {
            try
            {
                PurchaseOrders po = new PurchaseOrders();
                ListDictionary parameters = new ListDictionary();

                po.LoadFromSql("LoadUnPaidPurchaseOrder", parameters);
                return po;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("PurchaseOrders.cs", "GetUnPaidPOs", ex.StackTrace);
                throw new Exception("PurchaseOrders.GetUnPaidPOs(): " + ex.Message);
            }
        }

        public static PurchaseOrders GetPOByIDs(string ids)
        {
            try
            {
                PurchaseOrders p = new PurchaseOrders();
                if (ids == "")
                    ids = "-1";
                p.Where.ID.Value = ids;
                p.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.In;

                p.Query.Load();

                return p;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("PurchaseOrders.cs", "GetPOByIDs", ex.StackTrace);
                throw new Exception("PurchaseOrders.GetPOByIDs(): " + ex.Message);
            }
        }
    }
}
